package com.webserver.core;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 保存所有请求与对应的业务处理关系
 */
public class HandlerMapping {
    /*
        key:请求路径
        value:处理该请求的业务类及对应方法

        例如:
        key:/myweb/reg
        value:MethodMapping{
                controller:UserController
                method:reg()
               }
     */
    private static Map<String, MethodMapping> mapping = new HashMap<>();

    public static void main(String[] args) {

    }

    static {
        initMapping();

    }

    private static void initMapping() {
        /*
            扫描com.webserver.controller包中的所有类,并将所有被注解@controller
            的类实例化,然后将@RequestMapping标注的方法获取,并将这两项以一个
            MethodMapping表示. 最终将@RequestMapping注解传递的参数(请求路径)作为
            key,将这个MethodMapping作为value保存到mapping这个map
         */

        try {
            File dir = new File(HandlerMapping.class.getClassLoader().getResource("./com/webserver/controller").toURI());
            File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
            for (File sub : subs) {
                String fileName = sub.getName();
                String className = fileName.substring(0, fileName.indexOf("."));
                Class cls = Class.forName("com.webserver.controller." + className);
                if (cls.isAnnotationPresent(Controller.class)) {
                    Object controller = cls.newInstance();
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(RequestMapping.class)) {
                            //通过注解获取该方法对应的请求路径
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            String path = rm.value();
                            MethodMapping mm = new MethodMapping(controller, method);
                            mapping.put(path, mm);
                        }
                    }
                }

            }
            System.out.println(mapping.size());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据请求路径获取对应的业务处理类
     *
     * @param path
     * @return
     */
    public static MethodMapping getMethodMapping(String path) {
        return mapping.get(path);
    }

    public static class MethodMapping {
        //某个Controller实例
        private Object controller;
        //对应的业务处理方法
        private Method method;

        public MethodMapping(Object controller, Method method) {
            this.controller = controller;
            this.method = method;
        }

        public Object getController() {
            return controller;
        }

        public void setController(Object controller) {
            this.controller = controller;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }
    }
}
